package com.cdj.common.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.cdj.common.R;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;


/**
 * ---------------------------------------------------------<br />
 * desc：基础的刷新列表<br />
 * author：陈德基 <br />
 * date：2021/7/5 0:17<br />
 * email：18701434169@163.com<br />
 * qq: 781571323
 * wx: melody_2009
 * ---------------------------------------------------------<br />
 */
public class CommonRefreshListView<T> extends FrameLayout implements IRefreshAdapterHolder<T>{

    private SmartRefreshLayout refreshLayout;
    private RecyclerView recyclerView;
    private BaseQuickAdapter<T, BaseViewHolder> adapter;

    private IAdapterChildData<T> adapterChildData;

    public void setAdapterChildData(IAdapterChildData<T> adapterChildData) {
        this.adapterChildData = adapterChildData;
    }

    private RecyclerView.ItemDecoration decoration;

    public void setLayoutManager(RecyclerView.LayoutManager layoutManager) {
        recyclerView.setLayoutManager(layoutManager);
    }
    @Override
    public void setRefreshListener(OnRefreshListener refreshListener) {
        refreshLayout.setOnRefreshListener(refreshListener);
    }

    @Override
    public void setLoadMoreListener(OnLoadMoreListener loadMoreListener) {
        refreshLayout.setOnLoadMoreListener(loadMoreListener);
    }

    @Override
    public BaseQuickAdapter<T, BaseViewHolder> adapter() {
        return adapter;
    }

    public RecyclerView getRecyclerView() {
        return recyclerView;
    }

    public BaseQuickAdapter<T, BaseViewHolder> getAdapter() {
        return adapter;
    }

    public void setAdapter(BaseQuickAdapter<T, BaseViewHolder> adapter) {
        this.adapter = adapter;
        recyclerView.setAdapter(this.adapter);
    }

    public void setDecoration(RecyclerView.ItemDecoration decoration) {
        this.decoration = decoration;
    }

    public CommonRefreshListView(@NonNull Context context) {
        this(context, null);
    }

    public CommonRefreshListView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CommonRefreshListView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr, 0);
    }

    protected void initHolder() {
    }

    public CommonRefreshListView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        TypedArray t = context.obtainStyledAttributes(attrs, R.styleable.CommonRefreshListView);
        boolean enableRefresh = t.getBoolean(R.styleable.CommonRefreshListView_enable_refresh, true);
        boolean enableLoadmore = t.getBoolean(R.styleable.CommonRefreshListView_enable_loadMore, true);
        int layoutID = t.getResourceId(R.styleable.CommonRefreshListView_r_item_layout_id, 0);
        t.recycle();

        View view = LayoutInflater.from(context).inflate(R.layout.common_refresh_view, this, true);
        recyclerView = view.findViewById(R.id.common_recycleView);
        refreshLayout = view.findViewById(R.id.common_refresh);

        refreshLayout.setEnableRefresh(enableRefresh);
        refreshLayout.setEnableLoadMore(enableLoadmore);

        adapter = new BaseQuickAdapter<T, BaseViewHolder>(layoutID) {
            @Override
            protected void convert(BaseViewHolder helper, T item) {
//                fillItemData(helper, item);
                if (adapterChildData != null) {
                    adapterChildData.fillDataToView(helper, item);
                }
            }
        };

//        View emptyView = LayoutInflater.from(getContext()).inflate(R.layout.empty_layout, null, false);
//        adapter.setEmptyView(emptyView);

        recyclerView.setAdapter(this.adapter);
        if (decoration != null && recyclerView.getItemDecorationCount() == 0) {
            recyclerView.addItemDecoration(decoration);
        }
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        initHolder();
    }

//    protected abstract void fillItemData(BaseViewHolder helper, T item);

}
